package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ListView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ReceviceOrderLogAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.ReceviceOrderLog;
import com.isunland.managesystem.entity.ReceviceOrderLogListOriginal;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能: 已接单列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/3/19 17:03
 */
public class ReceviceOrderLogListFragment extends BaseListFragment
    implements ReceviceOrderLogAdapter.Callback {
  private static final String EXTRA_ID =
      ReceviceOrderLogListFragment.class.getSimpleName() + ".EXTRA_ITEM";
  private String mId;
  private ArrayList<ReceviceOrderLog> mList;
  private ReceviceOrderLogAdapter<ReceviceOrderLog> mAdapter;

  public static Fragment newInstance(String id) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ID, id);
    ReceviceOrderLogListFragment fragment = new ReceviceOrderLogListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setTitleCustom(R.string.hasReceivedOrderList);
    mId = getArguments().getString(EXTRA_ID);
    mList = new ArrayList<>();

    mAdapter = new ReceviceOrderLogAdapter<>(mActivity, mList, this);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setListAdapter(mAdapter);
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_ALREADY_RECEIVE_ORDER;
  }

  @Override public HashMap<String, String> getparameters() {
    if (mId == null) return super.getparameters();
    HashMap<String, String> params = new HashMap<>();
    params.put("mainId", mId);
    return params;
  }

  @Override public void resolveResponse(String json) {
    ReceviceOrderLogListOriginal original =
        new Gson().fromJson(json, ReceviceOrderLogListOriginal.class);
    if (original == null || original.getRows() == null || original.getResult() != Base.SUCCESS) {
      ToastUtil.show("获取列表失败");
      return;
    }
    mList.clear();
    mList.addAll(original.getRows());
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    ReceviceOrderLog item = mAdapter.getItem(position - mListview.getHeaderViewsCount());
    if (item != null && ReceviceOrderLog.TRUE_FINISH_RECEIVE_ORDER.equals(item.getIfConfirm())) {

      EvaluateLogListActivity.newInstance(this, mActivity, item, 0);
    } else {
      ToastUtil.show(R.string.notConfirmOrderHint);
    }
  }

  @Override public void onClickReceiveOrder(View v) {
    if (v == null) return;
    switch (v.getId()) {
      case R.id.tv_confirmOrder://确认订单
        if (v.getTag() instanceof ReceviceOrderLog) {
          ReceviceOrderLog item = (ReceviceOrderLog) v.getTag();
          if (item == null) return;
          if (ReceviceOrderLog.TRUE_FINISH_RECEIVE_ORDER.equals(item.getIfConfirm())) {
            //已接单,跳转评论列表
            EvaluateLogListActivity.newInstance(this, mActivity, item, 0);
          } else {
            //抢单
            volleyPostConfirmOrder(item.getId());
          }
        }
        return;

      default:
        break;
    }
  }

  /**
   * 抢单
   */
  private void volleyPostConfirmOrder(String id) {
    if (id == null || mId == null) return;
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_UPDATE_CONFIRM_RECEIVE_ORDER);
    HashMap<String, String> params = new HashMap<>();
    params.put("id", id);
    params.put("mainOrderId", mId);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show(R.string.receiveOrderFailure);
      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        Base original = new Gson().fromJson(arg0, Base.class);
        ToastUtil.show(original.getMessage());
        if (Base.SUCCESS == original.getResult()) {
          volleyPost();
        }
      }
    });
  }
}
